package com.ransj.iphone.view;

import com.ransj.iphone.IKey;
import com.ransj.iphone.IKeyListener;
import com.ransj.iphone.R;
import com.ransj.iphone.R.id;
import com.ransj.iphone.R.layout;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * 
 * @author ranshengju
 * 
 */
public class ShortCutsList extends AbsView implements OnClickListener{
	private static final String TAG = ShortCutsList.class.getSimpleName();
	private View mContentView;

	private ShortCutsList(Context context, LayoutParams params) {
		mMainView = LayoutInflater.from(context).inflate(
				R.layout.iphone_shortcuts, null);
		mContentView = mMainView.findViewById(R.id.iphone_shortcuts_list);
		View iphone = mMainView.findViewById(R.id.iphone_shortcuts_iphone);
		iphone.setTag(IKey.KEY_SC_IPHONE);
		iphone.setOnClickListener(this);
		View minus = mMainView.findViewById(R.id.iphone_shortcuts_minus);
		minus.setTag(IKey.KEY_SC_MINUS);
		minus.setOnClickListener(this);
		mWMParams = params;
	}

	private void generateList(Context context, int[] resId, int[] keyCodes) {
		int length = resId.length;
		LayoutInflater inflater = LayoutInflater.from(context);
		for (int i = 0; i < length; i++) {
			View item = inflater.inflate(R.layout.item_shortcuts, null);
			ImageView iv = (ImageView) item.findViewById(R.id.item_shortcuts_iv);
			iv.setImageResource(resId[i]);
			item.setLayoutParams(new LinearLayout.LayoutParams(
					android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
					android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
			iv.setTag(keyCodes[i]);
			iv.setOnClickListener(this);
			((ViewGroup)mContentView).addView(item);
		}
	}

	public static ShortCutsList createList(Context context,
			LayoutParams params, int[] resId, int[] keyCodes) {
		ShortCutsList temp = new ShortCutsList(context, params);
		temp.generateList(context, resId, keyCodes);
		return temp;
	}

	@Override
	public void onClick(View v) {
		if(mKeyListener != null){
			mKeyListener.onKeyEvent(this, (Integer) v.getTag());
		}
	}
	
	public void setOnKeyEventListener(IKeyListener listener) {
		mKeyListener = listener;
	}
	
}
